package com.st.controller.headend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.st.entity.Cart;
import com.st.entity.Order;
import com.st.entity.OrderGoods;
import com.st.service.ICartService;
import com.st.service.IShopOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/*购物车控制类*/
@Controller
@RequestMapping("cart")
public class CartController {
@Autowired
private ICartService cartService;
@Autowired
   private IShopOrderService shopOrderService;
    //添加购物车
    @RequestMapping("add")
    @ResponseBody
    public String add(Cart cart, HttpServletRequest req){
        HttpSession session= req.getSession();
        //获取用户id
       Integer userId= (Integer) session.getAttribute("userId");
       cart.setUserId(userId);
        System.out.println(cart);
       List<Cart> list=cartService.query(cart);
       if (list==null ||list.size()==0){
          Integer a= cartService.add(cart);
          if (a==1){return "1";}else {return "0";}
       }else {
           Cart c=list.get(0);
           Integer oldBuyCount=c.getBuyCount();
           c.setBuyCount(oldBuyCount+cart.getBuyCount());
           Integer b= cartService.update(c);
           if (b==1){return "1";}else {return "0";}
       }
    }

    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest req){
        ModelAndView mv=new ModelAndView();
        HttpSession session=req.getSession();
        String userPhone= (String) session.getAttribute("userPhone");
        Integer userId= (Integer) session.getAttribute("userId");
        Cart cart =new Cart();
        //判断页码变量是否为空，如果为空就赋值1 表示第一页
        if (cart.getPageNum() == null) {
            cart.setPageNum(1);
        }
        //一个查5条
        cart.setPageSize(6);
        //分页查询，注意该方法会对后面的第一个查询做分页
        PageHelper.startPage(cart.getPageNum(), cart.getPageSize());
        cart.setUserId(userId);
        List<Cart> list= cartService.query(cart);
        //把查出来的列表放到pageInfo中
        //分页信息，pageNum-页码 pageSize-单页记录数 ，pages-总页数
        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("userPhone",userPhone);
        mv.setViewName("/headend/cart_list");
        return mv;
    }
    @RequestMapping("upCount")
    public String upCount(Cart cart,HttpServletRequest req){
      HttpSession session =req.getSession();
        System.out.println(cart);
      Integer userId= (Integer) session.getAttribute("userId");
      cart.setUserId(userId);
      cartService.update(cart);
     return "redirect:/cart/list";
    }
    @RequestMapping("del")
    @ResponseBody
    public String del(Integer id){
      int a= cartService.del(id);
       if (a==1){
           return "1";
       }else {
           return "0";
       }
    }
    @RequestMapping("emptyCart")
    public ModelAndView emptyCart(String ids,HttpServletRequest req){
        HttpSession session=req.getSession();
        Integer userId= (Integer) session.getAttribute("userId");
        String userPhone= (String) session.getAttribute("userPhone");
       List<Cart>list= cartService.queryEmpty(ids);
        BigDecimal bigDecimal = new BigDecimal(0);
       for (int i=0;i<list.size();i++){
           BigDecimal price=list.get(i).getGoodsPrice();
         bigDecimal=bigDecimal.add(price.multiply(new BigDecimal(list.get(i).getBuyCount())));
       }
      List<Order> list1=  shopOrderService.createCartOrder(userId,userPhone,list);
        System.out.println(list);
        ModelAndView mv=new ModelAndView();
        mv.addObject("list",list1);
        mv.addObject("userPhone",userPhone);
        mv.addObject("money",bigDecimal);
        mv.setViewName("/headend/order_pay_cart");
        return mv;
    }
}
